/**
 * Created by Administrator on 15-1-7.
 * 系统管理使用
 */
require.config({
    baseUrl:PUBLIC + "/Admin/js/lib"
});
define(function(require){
    var $ = require('jquery');
    require('jquery.form');
    require('tips/msgbox');
    window.$ = window.jQuery = $;
    var c=require('../common');
    require('datetimepicker/datetimepicker');
    var dialog = require('bDialog/dialog');
    require('select');
    require('areaB');
    require('distinct');
    window.dialog=dialog;
    require('fileupload/fupload');

    $(function() {

        //下拉框初始化
        $('#cate_sel').selectbox();
        $('#user_sel').selectbox({callback:function(v){
            var selVal = v.attr('v');
            if(selVal){
                $('.upload-area').find('.uid').val(selVal);
                upload(selVal);
            }
        }});

        //上传图片
        function upload(uid){
            var path = $('#upload_paths').val();//存储文件夹
            var maxNum = $('#maxNums').val();//上传数量
            $('.fileupload').fupload({path: path, uid: uid, max: maxNum,input:'pic'});
        }
        if($('.fileupload').length) {
            var uid = $('.upload-area').find('.uid').val();
            upload(uid);
        }

        var areaSelect = null;
        $('#area').areaB({type:1,name:'area',input:"#area_path",cutstr:18,default_val:'请选择地区'},function () {
            areaSelect = this;
        });

        //日期选择
        $('#bus_stime,#der_stime').datetimepicker({
            datepicker:false,
            format:'H:i',
            step:5
        });
        $('#bus_etime,#der_etime').datetimepicker({
            datepicker:false,
            format:'H:i',
            step:5
        });

        var toUnicode = function (str) {
            return escape(str).replace(/%/g,"\\").toLowerCase();
        }

        //设置地图
        if($('#dituContent').length){
            var setLocation = function () {
                var areacode = $('#area_path').val();
                var address = $('#address').val();
                if(empty(areacode) || empty(address)) return false;

                var area = $('#province_input').html() + $('#city_input').html() + $('#area_input').html();
                address = area + address;

                $.get(URL+'/getLocation',{'address':address},function (data) {
                    if(data.status==1){
                        $('#map').val(data.data.lng+','+data.data.lat);
                        bdmap.init();
                        createBeautyMap();
                    } else {
                        c.error(data.info,2000);
                    }
                },'json');
            };

            var bdmap=null;
            var createBeautyMap = null;
            require(['creatmap'],function () {
                createBeautyMap = function () {
                    $('#dituContent').createmap({input:'#map',fn:function (address) {
                        $('#address').val(address.street + address.streetNumber);
                        var codeKey = address.province.substring(0,2) + '-' + address.city.substring(0,2) + '-' + address.district.substring(0,2);
                        codeKey = toUnicode(codeKey);
                        eval("var areacode = distinct[\""+codeKey+"\"]");

                        if(empty(areacode)){
                            $('#area_path').val('');
                        } else {
                            $('#area_path').val(areacode);
                        }

                        areaSelect.init();
                        //440304
                    }},function(t){
                        bdmap=t;
                    });
                };
                createBeautyMap();
            });

            //详细地址失去焦点事件
            $('#address').off().on('blur',function () {
                setLocation();
            });
        };

        //添加起送价
        $('.add_sprice').off().on('click',function(){
            var t = $(this);
            var html = '<div class="sprice cf mt10">';
            html +='<input name="smin_km[]" type="text" class="tb tb_50 fl" /><span class="fl">&nbsp;~&nbsp;</span>';
            html +='<input name="smax_km[]" type="text" class="tb tb_50 fl" /><span class="fl">&nbsp;公里&nbsp;&nbsp;</span>';
            html +='<input name="sprice[]" type="text" class="tb tb_50 fl" /><span class="fl">&nbsp;元</span>';
            html +='<a href="javascript:;" class="ml10 delCustomer">删除</a>';
            html +='</div>';
            t.before(html);
        });

        $('.form').delegate('.delCustomer','click',function(){
            $(this).parents('.sprice').remove();
        })

        //添加外送费
        $('.add_dprice').off().on('click',function(){
            var t = $(this);
            var html = '<div class="dprice cf mt10">';
            html +='<input name="dmin_km[]" type="text" class="tb tb_50 fl" /><span class="fl">&nbsp;~&nbsp;</span>';
            html +='<input name="dmax_km[]" type="text" class="tb tb_50 fl" /><span class="fl">&nbsp;公里&nbsp;&nbsp;</span>';
            html +='<input name="dprice[]" type="text" class="tb tb_50 fl" /><span class="fl">&nbsp;元</span>';
            html +='<a href="javascript:;" class="ml10 delCustomerd">删除</a>';
            html +='</div>';
            t.before(html);
        });

        $('.form').delegate('.delCustomerd','click',function(){
            $(this).parents('.dprice').remove();
        });

        //单选事件
        $('input[name="is_free"]').click(function(){
            var selVal = $(this).val();
            if(selVal == 2){
                $('.deliver').removeClass('hidden');
            }else{
                $('.deliver').addClass('hidden');
            }
        });

        //删除
        $('table .del').off().on('click', function () {
            var id = $(this).data('id');
            if (empty(id)) {
                c.error('请选择操作项！', 2000);
                return false;
            }

            dialog({
                title: '温馨提示', height: 100, icon: 'warning', content: '是否确定删除？', ok: function () {
                    $.get(URL + '/del?id=' + id, '', function (data) {
                        if (data.status == 1) {
                            c.success(data.info, 1000);
                            setTimeout(function () {
                                location.reload();
                            }, 1500);
                            return true;
                        } else {
                            c.error(data.info, 2000);
                            return false;
                        }
                    }, 'json');
                },
                cancel: function () {
                },
                okVal: '确定', cancelVal: '取消'
            });
        });

        //更改状态
        $('table .setPass').off().on('click', function () {
            var t = $(this);
            var id = t.data('id');
            var msg = t.html();
            if (empty(id)) {
                c.error('请选择操作项！', 2000);
                return false;
            }

            dialog({
                title: '温馨提示', height: 100, icon: 'warning', content: '是否' + msg + '该数据?', ok: function () {
                    $.get(URL + '/setStatus?id=' + id, '', function (data) {
                        if (data.status == 1) {
                            c.success(data.info, 1000);
                            setTimeout(function () {
                                location.reload();
                            }, 1500);
                            return true;
                        } else {
                            c.error(data.info, 2000);
                            return false;
                        }
                    }, 'json');
                },
                cancel: function () {
                },
                okVal: '确定', cancelVal: '取消'
            });
        });

        //添加、编辑商品分类
        $('.so_main').delegate('.add_shop_cate,.edit_shop_cate','click',function(){
            var t = $(this);
            var dialogTitle = '添加店铺分类';
            var url = URL+'/edit';
            var id = 0;
            if(t.hasClass('edit_shop_cate')){
                id = t.data('id');
                dialogTitle = '编辑店铺分类';
                url += '?id=' + id;
            }
            var d = dialog({title:dialogTitle,width:400,url:url,end:function(){
            },ok:function(){
                var body = this.c('body');
                var title = $.trim(body.find('input[name="title"]').val());
                var reg = /^\d/;

                if(empty(title)){
                    c.error('请输入分类名称!',2000);
                    $('input[name="title"]').focus();
                    return false;
                }
                if(title.length > 30){
                    c.error('分类名称限制为0~30个字符!',2000);
                    $('input[name="title"]').focus();
                    return false;
                }

                $.post(URL+'/update',{title:title,id:id},function(msg){
                    if(msg.status == 1) {
                        c.success(msg.info,1000,function(){
                            location.reload();
                        });
                        return true;
                    } else {
                        c.error(msg.info,2000);
                        return false;
                    }
                });
                return false;
            },cancel:function(){},okVal:'保存',cancelVal:'取消'});
            return false;
        });

        //添加、编辑店铺公告
        $('.so_main').delegate('.add_shop_notice,.edit_shop_notice','click',function(){
            var t = $(this);
            var dialogTitle = '添加分店公告';
            var url = URL+'/edit';
            var id = 0;
            if(t.hasClass('edit_shop_notice')){
                id = t.data('id');
                dialogTitle = '编辑分店公告';
                url += '?id=' + id;
            }
            var d = dialog({title:dialogTitle,width:600,url:url,end:function(){
                $('#shop_sel').selectbox();
            },ok:function(){
                var body = this.c('body');
                var shop_id = $('#shop_sel').val();
                var desc = $.trim($('textarea[name="desc"]').val());

                if(empty(shop_id)){
                    c.error('请选择所属分店!',2000);
                    return false;
                }
                if(empty(desc)){
                    c.error('请输入分店公告!',2000);
                    return false;
                }

                $.post(URL+'/update',{'desc':desc,'id':id,'shop_id':shop_id},function(msg){
                    if(msg.status == 1) {
                        c.success(msg.info,1000,function(){
                            location.reload();
                        });
                        return true;
                    } else {
                        c.error(msg.info,2000);
                        return false;
                    }
                });
                return false;
            },cancel:function(){},okVal:'保存',cancelVal:'取消'});
            return false;
        });

        //表单提交
        $('#detail_form').ajaxForm({
            beforeSubmit: checkForm,  // pre-submit callback
            success: complete,  // post-submit callback
            dataType: 'json'
        });

        function checkForm() {
            return true;
        }

        function complete(data) {
            if (data.status == 1) {
                c.success(data.info, 1000);
                if (data.data) {
                    setTimeout(function () {
                        location.href = data.data;
                    }, 1500);
                    return true;
                } else {
                    setTimeout(function () {
                        window.location.reload();
                    }, 1000);
                }
            } else {
                c.error(data.info, 2000);
                return false;
            }
        }
    })
});